package com.wavehi.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author WaveHi
 * @Date 16/9/2021 下午 7:36
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Calculator
 */
@WebServlet("/thirdHomeworkServlet")
public class ThirdHomeworkServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //统一字符集
        resp.setContentType("UTF-8");
        //获取请求参数
        String num1 = req.getParameter("num1");
        String num2 = req.getParameter("num2");
        String per = req.getParameter("per");
        //接收计算的结果
        Integer result = null;
        //判断运算条件是否满足
        boolean isCompleted = num1 != null && num2 != null && !"".equals(num1.trim()) && !"".equals(num2.trim());
        //计算
        if(isCompleted) {
            switch(per) {
                case "+" :
                    result = Integer.parseInt(num1) + Integer.parseInt(num2);
                    break;
                case "-" :
                    result = Integer.parseInt(num1) - Integer.parseInt(num2);
                    break;
                case "*" :
                    result = Integer.parseInt(num1) * Integer.parseInt(num2);
                    break;
                case "/" :
                    result = Integer.parseInt(num1) / Integer.parseInt(num2);
                    break;
                default :
                    throw new RuntimeException("Argument \'per\' is illegal!");
            }
        }
        //获取输出流
        PrintWriter writer = resp.getWriter();
        //回传页面
        writer.write("<!DOCTYPE HTML>");
        writer.write("<Html>");
        writer.write("<head>");
        writer.write("<title<Calculator</title>");
        writer.write("</head>");
        writer.write("<body>");
        writer.write("<form action=\"thirdHomeworkServlet\" method=\"get\">");
        if(num1 != null) {
            writer.write("<input type=\"text\" name=\"num1\" id=\"num1\" value=\"" + num1 + "\">");
        }else {
            writer.write("<input type=\"text\" name=\"num1\" id=\"num1\">");
        }
        writer.write("<select name=\"per\" id=\"per\">");
        if("+".equals(per)) {
            writer.write("<option name=\"per\" value=\"\\+\" selected=\"selected\">+</option>");
        }else{
            writer.write("<option name=\"per\" value=\"+\">+</option>");
        }
        if("-".equals(per)) {
            writer.write("<option name=\"per\" value=\"-\" selected=\"selected\">-</option>");
        }else {
            writer.write("<option name=\"per\" value=\"-\">-</option>");
        }
        if("*".equals(per)) {
            writer.write("<option name=\"per\" value=\"*\" selected=\"selected\">*</option>");
        }else {
            writer.write("<option name=\"per\" value=\"*\">*</option>");
        }
        if("/".equals(per)) {
            writer.write("<option name=\"per\" value=\"/\" selected=\"selected\">/</option>");
        }else {
            writer.write("<option name=\"per\" value=\"/\">/</option>");
        }

        writer.write("</select>");
        if(num2 != null) {
            writer.write("<input type=\"text\" name=\"num2\" id=\"num2\" value=\"" + num2 + "\">");
        }else {
            writer.write("<input type=\"text\" name=\"num2\" id=\"num2\">");
        }
        writer.write("<input type=\"submit\" value=\"=\">");
        if(result != null) {
            writer.write("<input type=\"text\" name=\"result\" id=\"result\" value=\"" + result + "\" disabled>");
        }else {
            writer.write("<input type=\"text\" name=\"result\" id=\"result\" disabled>");
        }
        writer.write("</form>");
        writer.write("</body>");
        writer.write("</Html>");
//    <form action="">
//    <input type="text" name="num1" id="num1">
//    <select name="per" id="per">
//        <option value="+">+</option>
//        <option value="-">-</option>
//        <option value="*">*</option>
//        <option value="/">/</option>
//    </select>
//    <input type="text" name="num2" id="num2"">
//                <input type="text" name="result" id="result">
//</form>
    }
}
